package form;

import java.util.List;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

public final class VisaForm
{
    private static void cardNumberIsValid(String cardNumber) throws FormValidationException
    {		
        if(cardNumber != null)
        {
            if(!cardNumber.matches("[0-9]{16}"))
            {
                throw new FormValidationException("Please enter a valid card number.");
            }
        } 
        else
        {
                throw new FormValidationException("Please enter a card number.");
        }
    }
    
    private static void cardCryptIsValid(String cardCrypt) throws FormValidationException
    {		
        if(cardCrypt != null)
        {
            if(!cardCrypt.matches("[0-9]{3}"))
            {
                throw new FormValidationException("Please enter a valid card crypt.");
            }
        } 
        else
        {
            throw new FormValidationException("Please enter a card crypt.");
        }
    }
        
    public static List<String> validateForm(HttpServletRequest request)
    {
        List<String> errors = new ArrayList<>();
        
        try
        {
            cardNumberIsValid(request.getParameter("card_number"));
        }
        catch(FormValidationException fve)
        {
            errors.add(fve.getMessage());
        }
        
        try
        {
            cardCryptIsValid(request.getParameter("card_crypt"));
        }
        catch(FormValidationException fve)
        {
            errors.add(fve.getMessage());
        }
        
        return errors;
    }
}